package com.example.redissontest.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author rfuser
 */
@Configuration
@ConfigurationProperties(prefix = "redisson")
public class RedissonConfig {

    private String host;
    private Integer port;
    private Integer database;
    private String password;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public Integer getDatabase() {
        return database;
    }

    public void setDatabase(Integer database) {
        this.database = database;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Bean
    public RedissonClient redissonClient() {
        // 创建配置实例
        Config config = new Config();
        // 使用单机模式
        config.useSingleServer()
              .setAddress(this.host+":"+this.port)
              .setDatabase(this.database)
              .setPassword(this.password);

        // 根据配置创建Redisson客户端实例
        return Redisson.create(config);
    }



}
